<?php
if(!defined("IN_PHPSCUP"))exit("hacking");
class Upfile
{

  var   $_root_dir          = '';//上传根目录
	
  var   $_allowed_file_size = "";//允许上传的文件的大小
  
  var   $_allowed_file_type = "";//允许上传的文件类型
  
  var   $_up_path           = "";//上传路径
  
  var   $_file_msg          = array();//文件的相关信息
  
  var   $_upfile_type       = '';//上传文件类型
  
  var   $_is_waterimg       = true;//是否加水印
  
  var   $_is_make_thumb       = true;  

 function __construct($path,$is_waterimg=true)
    {	
	
        $this->Upfile($path,$is_waterimg);
				
    }
 //$path指上传的路径	   
 function Upfile($path,$is_waterimg=true)
    {	
        $this->_allowed_file_type = explode('|', trim(UPFILE_TYPES));	    
        $this->_root_dir = (UPFILE_PATH=='')?(ROOT_PATH . '/upfiles'):(ROOT_PATH .'/'. UPFILE_PATH);							
		$this->_up_path = preg_match("/\./i",$path)?'temp':$path;
		$this->_is_waterimg = $is_waterimg;
		$this->_is_make_thumb = $is_waterimg;			

		 @include_once(ROOT_PATH ."/lib/image.lib.php");					
	
			
    }	   
	   

 function _upfile($upfilename,$filemsg='')
    {	
	    if(!$upfilename)
	    {
		     $this->_error("Upfile no exist");
	    }	
		
		/*if(strtolower(strrchr(basename($upfilename),"."))!='.tmp')
		{
		     $this->_error("upfile error");
		}		*/		
		
	    if(!empty($filemsg))
		{
		     $this->_file_msg = $filemsg;
		     $this->_up_path = $filemsg['path'];
		}
		
	    if(!($this->_upfile_type = strtolower(strrchr($this->_file_msg['name'],"."))))
	    {
		     $this->_error("Upfile no postfix");
	    }	
		
	    $up_path = get_dir($this->_root_dir.'/'.$this->_up_path);
		
	   if($up_path=='false')
	   {
			 $this->_error('Can not create this path');				 
	   }
	   
	   if(!is_writable($up_path))
	   {
			 $this->_error('Can not write this path');				 
	   }		
			
	   $this->_limit();
				
	   $temp_filename = "".$this->_file_msg['user_name']."_".date("YmdHms_",time()).rand(1000,10000).$this->_upfile_type;
	   	   
	   $new_file = $up_path . '/' .$temp_filename;
	   $check = false;	   
	   if(@move_uploaded_file($upfilename,$new_file))
	   {
		     @chmod($new_file, 0777);
		     $check = true;
	   }
       
	   if($check)
	   {      
	          if($this->_is_waterimg&&IS_WATERIMG)
			  {
			  
				 $is_icon = 1;
				   
				 if(isset($_POST['waterimg']))
				 {
				    $is_icon = intval($_POST['waterimg']);
			     }
				 if(isset($_GET['waterimg']))
				 {
				    $is_icon = intval($_GET['waterimg']);
				 }		  
			     if(is_file(ROOT_PATH . '/' . UPFILE_PATH . '/' . WATERIMG_DIR)&&$is_icon)
				 {
				  Image:: water_mark($new_file,ROOT_PATH . '/' . UPFILE_PATH . '/' . WATERIMG_DIR);	
				 }
				 else
				 {
			        if(is_file(ROOT_PATH . '/data/images/logo.gif')&&$is_icon)
					{
					Image:: water_mark($new_file,ROOT_PATH . '/data/images/logo.gif');						
					}				 
				 }
									
					//Image::cutimg($new_file,$endimgurl,$x,$y,$endimg_w,$endimg_h,$border_w,$border_h,$scale=100,$fix=0);					
			  }
             $thumb_width = THUMB_WIDTH;
			 $thumb_height = THUMB_HEIGHT; 			  
			 if(isset($_POST['thumb_width']))
			 {
				  $thumb_width = intval($_POST['thumb_width']);
			 }
			 if(isset($_GET['thumb_width']))
			 {
				  $thumb_width = intval($_GET['thumb_width']);
			 }				  
			 if(isset($_POST['thumb_height']))
			 {
				  $thumb_height = intval($_POST['thumb_height']);
			 }
			 if(isset($_GET['thumb_height']))
			 {
				  $thumb_height = intval($_GET['thumb_height']);
			 }			  
			  
			  if($this->_is_make_thumb&&$thumb_width)
			  {			  
			     Image:: make_thumb($new_file, $new_file, $thumb_width, $thumb_height );		  
			  }

			  
	          unset($array);
	          $array['path'] = $this->_up_path . '/' . $temp_filename;
	          $array['size'] = $this->_file_msg['size'];
			  $array['type'] = $this->_upfile_type;			  
	          return $array;
	   }
	   else
	   {
			 $this->_error('Up fail');				 
	   }
				
    }

  //批量上传文件，$user_name用户名，$is_real=1表示返的文件路径值为网站全路径值
 function _batch_upfile($user_name = '',$is_real = 0)
    {	
	     $filedata=array();
	     $this->_file_msg['user_name'] = $user_name;	

         foreach( $_FILES AS $key=>$value )
	     {     
		       if(!preg_match("/^[A-Za-z]+[0-9]+$/i",$key))
			   {
			    $this->_error('exit hacking');
 			   } 			   
			   $i = intval(preg_replace ("/^[A-Za-z]+/", "", $key));
			   			   
		       if(is_array($value))
		       {
			   
	 				if(!$value['tmp_name'])
					{
					// $this->_error("请选择上传的图片");
					  echo '<script> alert("请选择上传的图片");window.setTimeout("javascript:history.back()", "1");</script>';exit;
					}		   
			         $upfilename=$value['tmp_name'];
			         $this->_file_msg['name']=$value['name'];
			         $this->_file_msg['size']=$value['size'];
					 if($this->_file_msg['name'])
					 {					 
					 	
						  $filedata[$i] = $this->_upfile($upfilename);
						  if(empty($filedata[$i]))
						  {
						   unset($filedata[$i]);
						  }
						  else
						  {
						       if($is_real==1)
							   {
							       $filedata[$i]['path'] = SITE_URL.'/'.((UPFILE_PATH=='')?'upfiles':UPFILE_PATH).'/'.$filedata[$i]['path'];
							   }
						  }
						  
											 
					 } 
					
		       }
		       		  
		 }
         return $filedata;
				
    }	
	
  //得到文件的真实路径
 function _real_path($array = array())
    {	 
	     if(empty($array))
		 {
		  return false;
		 }		 
         foreach($array as $key=> $value)
		 {   
		     if(isset($value['path']))
			 { 
		     $array[$key]['path'] = SITE_URL .'/'.((UPFILE_PATH=='')?'upfiles':UPFILE_PATH).'/' . $value['path'];
			 }
		 }
		 return $array;
    }	   

  //图片加水印
 function _water_image()
    {	 
	     
        
    }	   
		   
	   

  //一些限制上传的条件函数
 function _limit()
    {	
		   
	     $this->_file_msg['size']=abs($this->_file_msg['size']);

	   	 if($this->_file_msg['size']>MAX_FILE_SIZE*1024*1024)
		 {	
			 $this->_error('File size error!');				 
		 }			 
		 

		 if(in_array($this->_upfile_type,array('.php','.asp','.aspx','.jsp','.cgi')))
		 {
			 $this->_error('This file not enable up!');				 
		 }
	   	 if($this->_file_msg['name']&&!preg_match("/(".UPFILE_TYPES.")$/i",$this->_file_msg['name']))
		 {	
			 $this->_error('File format error!');				 
		 }	
		 			
    }	   
	   

  //错误信息显示
 function _error($msg)
    {	 
		 $notice = &get_message(array('msg'=>"{$msg}",'url'=>'','title'=>'Notice'));
		 $notice ->display();				
    }	   
	   

}



?>